/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Network;

import java.io.*;
import java.net.URL;
import java.lang.Float;

public class Autopatcher
{
    static String url_str;
    static String version = "1.0", highestVersion;
    static URL url;

    public static void main( String[] args )
    {
        readFile();
        while( Float.valueOf(version) < Float.valueOf(highestVersion) )
        {
            downloadFiles( url_str , version );
        }
    }

    public static void readFile()
    {
        File file = new File("Version.txt");

        if( !file.exists() )
        {
            url_str = "http://www.kunst-aus-krempel.de/updateTo";
        }

        try
        {
            BufferedReader reader = new BufferedReader( new FileReader( file ) );
            url_str = reader.readLine();

            version = reader.readLine();

            if( url_str == null)
                url_str = "http://www.kunst-aus-krempel.de/updateTo";
            if( version == null)
                version = "1.0";

            String line = "notNull";
            URL readURL = new URL(url_str + "1.0");
            InputStream fileIn = readURL.openStream();
            BufferedReader fileReader = new BufferedReader( new InputStreamReader(fileIn) );
            while(line != null )
            {
                if( line.equals("highestVersion") == true )
                {
                    highestVersion = fileReader.readLine();
                }
                line = fileReader.readLine();
            }
            System.out.println("highest Version: " + highestVersion);
            System.out.println("actual Version: " + version);
        }
        catch( Exception e )
        {
            System.out.println("Fehler in Funktion: 1 " + e.toString() );
        }

    }

    public static void downloadFiles( String url_ , String version_ )
    {
        try
        {
            url = new URL( url_str + version + ".txt");

            InputStream in = url.openStream();

            BufferedReader reader = new BufferedReader( new InputStreamReader(in) );

            String temp_url = reader.readLine();
            String temp_name = reader.readLine();

            while( temp_url != null )
            {
                if( temp_url.compareToIgnoreCase("updateTo") == 0 )
                {
                    setVersion(temp_name);
                }
                else if( temp_url.compareToIgnoreCase("highestVersion") == 0 )
                {

                }
                else
                {
                    downloadFile( temp_url , temp_name );
                    System.out.println(temp_url);
                }

                temp_url = reader.readLine();
                temp_name = reader.readLine();
            }

            in.close();
            System.out.println("Version: " + version);
        }
        catch( Exception e )
        {
            System.out.println( "Fehler in Funktion 2: " + e.toString());
        }
    }

    static void downloadFile( String url_ , String name )
    {
        try
        {
            BufferedInputStream in = new BufferedInputStream( new URL(url_).openStream());
            FileOutputStream out = new FileOutputStream( name );
            BufferedOutputStream output = new BufferedOutputStream( out , 1);
            byte data[] = new byte[1];

            while( in.read( data , 0 , 1 ) >= 0 )
            {
                output.write(data);
            }
            output.close();
            out.close();
            in.close();
        }
        catch( Exception e )
        {
            System.out.println( "Fehler in Funktion 3: " + e.toString());
        }
    }

    static void setVersion( String version_str )
    {
        System.out.println(url_str + version_str);
        try
        {
            BufferedWriter writer = new BufferedWriter( new FileWriter( new File("Version.txt") ) );
            writer.write(url_str);
            writer.newLine();
            writer.write(version_str);
            writer.close();

            version = version_str;
        }
        catch( Exception e )
        {
            System.out.println( "Fehler in Funktion 4: " + e.toString() );
        }


    }
}